package com.cloudsigns.factorymanager2.engineering.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringEditAbnormalFeedbackFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringEditNormalFeedbackFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringEditReplyFeedbackFragment;
import com.cloudsigns.factorymanager2.engineering.fragment.EngineeringEditSuggestFeedbackFragment;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

public class EngineeringEditFeedbackFragmentActivity extends FragmentActivity implements View.OnClickListener{
    private RelativeLayout rl_back;
    private RelativeLayout rl_normal_feedback,rl_abnormal_feedback,rl_suggest_feedback,rl_abnormal_reply;
    private TextView tv_normal_feedback,tv_abnormal_feedback,tv_suggest_feedback,tv_abnormal_reply;
    private View view_normal_feedback,view_abnormal_feedback,view_suggest_feedback,view_abnormal_reply;
    private CustomViewPager viewpager;

    public static final int NORMAL=0;
    public static final int ABNORMAL=1;
    public static final int SUGGEST=2;
    public static final int REPLY=3;
    public int currentIndex = NORMAL;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private EngineeringEditNormalFeedbackFragment normalFeedbackFragment;
    private EngineeringEditAbnormalFeedbackFragment abnormalFeedbackFragment;
    private EngineeringEditSuggestFeedbackFragment suggestFeedbackFragment;
    private EngineeringEditReplyFeedbackFragment replyFeedbackFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineering_edit_feedback);

        initView();
        initData();
        setListener();
    }

    protected void initView() {
        rl_back=findViewById(R.id.rl_back);
        rl_normal_feedback=findViewById(R.id.rl_normal_feedback);
        rl_abnormal_feedback=findViewById(R.id.rl_abnormal_feedback);
        rl_suggest_feedback=findViewById(R.id.rl_suggest_feedback);
        rl_abnormal_reply=findViewById(R.id.rl_abnormal_reply);
        tv_normal_feedback=findViewById(R.id.tv_normal_feedback);
        tv_abnormal_feedback=findViewById(R.id.tv_abnormal_feedback);
        tv_suggest_feedback=findViewById(R.id.tv_suggest_feedback);
        tv_abnormal_reply=findViewById(R.id.tv_abnormal_reply);
        view_normal_feedback=findViewById(R.id.view_normal_feedback);
        view_abnormal_feedback=findViewById(R.id.view_abnormal_feedback);
        view_suggest_feedback=findViewById(R.id.view_suggest_feedback);
        view_abnormal_reply=findViewById(R.id.view_abnormal_reply);
        viewpager=findViewById(R.id.viewpager);
    }

    protected void initData() {
        normalFeedbackFragment=new EngineeringEditNormalFeedbackFragment();
        abnormalFeedbackFragment=new EngineeringEditAbnormalFeedbackFragment();
        suggestFeedbackFragment=new EngineeringEditSuggestFeedbackFragment();
        replyFeedbackFragment=new EngineeringEditReplyFeedbackFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(normalFeedbackFragment);
        listFragments.add(abnormalFeedbackFragment);
        listFragments.add(suggestFeedbackFragment);
        listFragments.add(replyFeedbackFragment);


        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(3);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    protected void setListener() {
        rl_back.setOnClickListener(this);
        rl_normal_feedback.setOnClickListener(this);
        rl_abnormal_feedback.setOnClickListener(this);
        rl_suggest_feedback.setOnClickListener(this);
        rl_abnormal_reply.setOnClickListener(this);
    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case NORMAL:
                rl_normal_feedback.setSelected(true);
                rl_abnormal_feedback.setSelected(false);
                rl_suggest_feedback.setSelected(false);
                rl_abnormal_reply.setSelected(false);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_suggest_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_bbbbbb));

                view_normal_feedback.setVisibility(View.VISIBLE);
                view_abnormal_feedback.setVisibility(View.GONE);
                view_suggest_feedback.setVisibility(View.GONE);
                view_abnormal_reply.setVisibility(View.GONE);
                break;

            case ABNORMAL:
                rl_normal_feedback.setSelected(false);
                rl_abnormal_feedback.setSelected(true);
                rl_suggest_feedback.setSelected(false);
                rl_abnormal_reply.setSelected(false);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_suggest_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_bbbbbb));

                view_normal_feedback.setVisibility(View.GONE);
                view_abnormal_feedback.setVisibility(View.VISIBLE);
                view_suggest_feedback.setVisibility(View.GONE);
                view_abnormal_reply.setVisibility(View.GONE);
                break;

            case SUGGEST:
                rl_normal_feedback.setSelected(false);
                rl_abnormal_feedback.setSelected(false);
                rl_suggest_feedback.setSelected(true);
                rl_abnormal_reply.setSelected(false);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_suggest_feedback.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_bbbbbb));

                view_normal_feedback.setVisibility(View.GONE);
                view_abnormal_feedback.setVisibility(View.GONE);
                view_suggest_feedback.setVisibility(View.VISIBLE);
                view_abnormal_reply.setVisibility(View.GONE);
                break;

            case REPLY:
                rl_normal_feedback.setSelected(false);
                rl_abnormal_feedback.setSelected(false);
                rl_suggest_feedback.setSelected(false);
                rl_abnormal_reply.setSelected(true);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_suggest_feedback.setTextColor(getResources().getColor(R.color.color_bbbbbb));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));

                view_normal_feedback.setVisibility(View.GONE);
                view_abnormal_feedback.setVisibility(View.GONE);
                view_suggest_feedback.setVisibility(View.GONE);
                view_abnormal_reply.setVisibility(View.VISIBLE);
                break;

            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_back:
                finish();
                break;
            // 正常反馈
            case R.id.rl_normal_feedback:
                if (currentIndex != NORMAL) {
                    currentIndex = NORMAL;
                    viewpager.setCurrentItem(NORMAL);
                    showSelectedOption(NORMAL);
                }
                break;
            // 异常反馈
            case R.id.rl_abnormal_feedback:
                if (currentIndex != ABNORMAL) {
                    currentIndex = ABNORMAL;
                    viewpager.setCurrentItem(ABNORMAL);
                    showSelectedOption(ABNORMAL);
                }
                break;
            // 建议
            case R.id.rl_suggest_feedback:
                if (currentIndex != SUGGEST) {
                    currentIndex = SUGGEST;
                    viewpager.setCurrentItem(SUGGEST);
                    showSelectedOption(SUGGEST);
                }
                break;
            // 异常回复
            case R.id.rl_abnormal_reply:
                if (currentIndex != REPLY) {
                    currentIndex = REPLY;
                    viewpager.setCurrentItem(REPLY);
                    showSelectedOption(REPLY);
                }
                break;
        }
    }
}
